package org.agile2soft.nexoma.core.tapestry.pages.internaluser;

import org.agile2soft.nexoma.core.CoreConstants;
import org.agile2soft.nexoma.core.controller.InternalUserController;
import org.agile2soft.nexoma.core.controller.ProfileController;
import org.agile2soft.nexoma.core.entity.InternalUser;
import org.agile2soft.nexoma.core.entity.Profile;
import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.ValueEncoder;
import org.apache.tapestry5.annotations.Mixin;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.beaneditor.BeanModel;
import org.apache.tapestry5.beaneditor.Validate;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.BeanModelSource;

import br.com.arsmachina.tapestrycrud.base.BaseEditPage;
import br.com.arsmachina.tapestrycrud.hibernatevalidator.mixins.HibernateValidatorMixin;

public class EditInternalUser extends BaseEditPage<InternalUser,Integer> {

    @Mixin
    @SuppressWarnings("unused")
    private HibernateValidatorMixin hibernateValidatorMixin;

    @Inject
    private InternalUserController internalUserController;

    @Inject
    private BeanModelSource beanModelSource;

    @Inject
    private Messages messages;

    @Override
    public BeanModel<InternalUser> getBeanModel() {
        return beanModelSource.createEditModel(InternalUser.class,messages);
    }

    @Inject
    private ProfileController profileController;

    protected void prepareObjectForSaveOrUpdate() {
        getController().save(getObject());
    }

    public ValueEncoder<Profile> getProfileVE() {
        return getValueEncoder(Profile.class);
    }

    public SelectModel getProfileSM() {
        return getSelectModelFactory().create(Profile.class,profileController.findAll());
    }

    @Validate("required")
    public Profile getProfile() {
        return getObject().getProfile();
    }

    public void setProfile(Profile profile) {
        getObject().setProfile(profile);
    }

//    @Component
//    private Kaptcha kaptcha;

    @Persist
    @Property
    private boolean isValidKaptcha;

    @Override
    protected void validateObject(InternalUser object, Form form) {

        // Validate that the document number is not assigned to another internal user
        final InternalUser internalUser = internalUserController.findByDocNumber(object.getDocumentNumber());
        if ( internalUser!=null ) {
            if ( !internalUser.equals(object) ) {
                // The document number is already used by another internal user
                addError("documentNumber", messages.get(CoreConstants.MESSAGE_ERROR_DNI_ALREADY_INUSE));
            }
        }

//        //Validate the Kaptcha box.
//        if ( !isValidKaptcha ) {
//            addError("kaptcha",messages.get(CoreConstants.MESSAGE_ERROR_KAPTCHA));
//        }

    }

}
